﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Data;
using System.Reflection;

namespace fap
{
    class 通用操作
    {

        private static 通用操作 _instance = null;
        public static 通用操作 instance
        {
            get
            {

                if (null == _instance)
                {
                    _instance = new 通用操作();
                }
                return _instance;
            }
            set
            {
                _instance = value;
            }

        }
        /// <summary>
        /// 判断输入的字符串是否只包含数字和英文字母
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public  bool IsNumAndEnCh(string input)
        {
            string pattern = @"^[A-Za-z0-9]+$";
            Regex regex = new Regex(pattern);
            return regex.IsMatch(input);
        }
        /// <summary>
        /// 判断输入的字符串是否只包含数字和英文字母和汉字
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public bool IsNumAndEnChand汉字(string input)
        {
            string pattern = @"^[a-zA-Z0-9\u4e00-\u9fa5]+$";
            Regex regex = new Regex(pattern);
            return regex.IsMatch(input);
        }

        /// <summary>
        /// 获取指定文件夹下当前目录下的所有文件夹
        /// </summary>
        /// <param name="Path">文件路径</param>
        /// <returns>文件夹下文件名</returns>
        public  string[] Read_DirectoryNames(string Path)
        {
            try
            {

                List<string> arr = new List<string>();
                DirectoryInfo dir = new DirectoryInfo(Path);
                string name;

                foreach (DirectoryInfo directoryName in dir.GetDirectories())
                {
                    name = directoryName.Name;
                    arr.Add(name);
                }

                string[] str = new string[arr.Count];
                for (int i = 0; i < arr.Count; i++)
                {
                    str[i] = arr[i].ToString();
                }
                return str;

            }
            catch
            {

                return null;
            }
        }


        /// <summary>
        /// 获取指定文件夹下当前目录下的所有文件
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>文件夹下文件名</returns>
        public  string[] Read_FilNames(string filePath)
        {
            try
            {
             
                List<string> arr = new List<string>();
                arr.Add("空");
        DirectoryInfo dir = new DirectoryInfo(filePath);
                string name;
                //遍历文件夹
                foreach (FileInfo file in dir.GetFiles())
                {
                    name = Path.GetFileNameWithoutExtension(file.Name);
                    arr.Add(name);
                }
                string[] str = new string[arr.Count];
                for (int i = 0; i < arr.Count; i++)
                {
                    str[i] = arr[i].ToString();
                }
                return str;

            }
            catch
            {

                return null;
            }

        }

  
        public void rich显示字符(RichTextBox richTextBox, string mes, Color Sp)
        {
            try
            {
               
                 
                        if (richTextBox.Lines.Count() > 1000)
                        {
                            richTextBox.Clear();
                        }
                        string TimeAdd;
                        string str;
                        TimeAdd = DateTime.Now.ToString("HH:mm:ss");
                        str = TimeAdd + ":" + mes + "\n"; ;
                        richTextBox.SelectionColor = Sp;
                        if (str == null)
                        {
                            str = new string(new char[] { });

                        }

                        richTextBox.AppendText(str);
                    richTextBox.Show();
                        return;



              




            }
            catch 
            {



            }

        }



        #region 计算

        public void 已知圆心角度求坐标(double 半径, double 角度, double 中心X, double 中心Y, out double x, out double y)

        {





            x = 中心X + 半径 * Math.Cos(角度 * Math.PI / 180);
            y = 中心Y + 半径 * Math.Sin(角度 * Math.PI / 180);





        }

        public void 已知坐标点求圆心(double 半径, double 角度, double X, double Y, out double 中心x, out double 中心y)

        {





            中心x = X - 半径 * Math.Cos(角度 * Math.PI / 180);
            中心y = Y - 半径 * Math.Sin(角度 * Math.PI / 180);





        }
        public void 已知坐标求圆心(double 半径, double 角度, double 当前坐标X, double 当前坐标Y, out double X, out double Y)

        {





            X = 当前坐标X - 半径 * Math.Cos(角度 * Math.PI / 180);
            Y = 当前坐标Y - 半径 * Math.Sin(角度 * Math.PI / 180);





        }

        public void 已知一点求另一个点(double x, double y, double angle, double dist, out double xx, out double yy)

        {
            angle = (angle) * Math.PI / 180;
            xx = Math.Cos(angle) * dist + x;
            yy = y + dist * Math.Sin(angle);

            Thread.Sleep(10);

        }

   
        public void getcir(double P1X, double P1Y, double P2X, double P2Y, double P3X, double P3Y, out double R, out double x, out double y)
        {

            double a = 2 * (P2X - P1X);
            double b = 2 * (P2Y - P1Y);
            double c = P2X * P2X + P2Y * P2Y - P1X * P1X - P1Y * P1Y;
            double d = 2 * (P3X - P2X);
            double e = 2 * (P3Y - P2Y);
            double f = P3X * P3X + P3Y * P3Y - P2X * P2X - P2Y * P2Y;
            x = (b * f - e * c) / (b * d - e * a);
            y = (d * c - a * f) / (b * d - e * a);
            double r = (float)Math.Sqrt((double)((x - P1X) * (x - P1X) + (y - P1Y) * (y - P1Y)));
            R = r;






        }


        #endregion



        #region 枚举转换DataTable
        /// <summary>
        /// 枚举绑定combox
        /// </summary>
        /// <returns></returns>
        public static DataTable GetDataTable<T>()
        {
            Type t = typeof(T); //创建类型
            FieldInfo[] fieldinfo = t.GetFields(); //获取字段信息对象集合

            DataTable table = new DataTable();
            table.Columns.Add("Name", typeof(String));
            table.Columns.Add("Value", typeof(Int32));

            foreach (FieldInfo field in fieldinfo)
            {
                if (!field.IsSpecialName)
                {
                    DataRow row = table.NewRow();
                    row[0] = field.Name; //获取文本字段
                    row[1] = (int)field.GetRawConstantValue(); //获取int数值
                    table.Rows.Add(row);
                }
            }
            return table;
        }


        #endregion

    }



    class 委托
    {
        private static 委托 _instance = null;
        public static 委托 instance
        {
            get
            {

                if (null == _instance)
                {
                    _instance = new 委托();
                }
                return _instance;
            }
            set
            {
                _instance = value;
            }

        }

       public void  委托主线程()
        {
            //委托给有权限的线程
            //Dispatcher.Invoke((Action)(() =>
            //{

            //}));

            ///form委托给是UI
           // this.Invoke((MethodInvoker)delegate { });
        }

    }


}
